package com.atguigu.srb.base.response;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class R {
    //响应对象状态码  0代表成功  1表示默认失败 其他代表失败
    private Integer code;
    //状态码的描述文本
    private String msg;
	//接收成功的响应数据(item,integralGrade)(items,list)
    private Map<String, Object> data = new HashMap();


    //获取一个代表成功的对象
    public static R ok() {
        R r = new R();
        r.setCode(ResponseEnum.SUCCESS.getCode());
        r.setMsg(ResponseEnum.SUCCESS.getMsg());
        return r;
    }

    //获取一个代表失败的对象
    public static R error() {
        R r = new R();
        r.setCode(ResponseEnum.ERROR.getCode());
        r.setMsg(ResponseEnum.ERROR.getMsg());
        return r;
    }
    //根据传入的枚举类对象 创建R对象
    public static R setResult(ResponseEnum responseEnum) {
        R r = new R();
        r.setCode(responseEnum.getCode());
        r.setMsg(responseEnum.getMsg());
        return r;
    }
    //向map中存入成功的响应数据
    public R data(String key, Object val) {
        this.data.put(key, val);
        return this;
    }
    //使用传入的map替换data默认map
    public R data(Map<String,Object>map) {
        this.data = map;
        return this;
    }
    //修改code的方法：支持链式调用
    public R code(Integer code) {
        this.code = code;
        return this;
    }

    //修改msg的方法：支持链式调用
    public R msg(String msg) {
        this.msg = msg;
        return this;
    }
}